//
//  AddressPickerView.m
//  YunGui
//
//  Created by Hanen 3G 01 on 16/4/11.
//  Copyright © 2016年 hanen. All rights reserved.
//

#import "AddressPickerView.h"

@interface AddressPickerView ()<UIPickerViewDelegate,UIPickerViewDataSource>
{
    UIView *_placeView;
    UIPickerView *_placePickerView;
}

@property (nonatomic, strong) NSMutableArray *allRegionArray;
@property (nonatomic, strong) NSMutableArray *provinceArray;
@property (nonatomic, strong) NSMutableArray *cityArray;
@property (nonatomic, strong) NSMutableArray *regionArray;
@property (nonatomic, strong) NSMutableArray *selectedArray;

@end

@implementation AddressPickerView

- (id)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self initData];
        [self addPickerView];
    }
    return self;
}

- (void)initData
{
    int isCache;
    NSString *currentTime = [DateTool getCurrentDate];
    NSString *timeString = [[NSUserDefaults standardUserDefaults] objectForKey:@"getRegionTime"];
    //    NSLog(@"%@%@",currentTime,timeString);
    if (timeString != nil) {
        NSInteger diffTime = [DateTool getTimesDiffTimeFrom:timeString To:currentTime];
        if (diffTime > 24) {
            isCache = 1;
        }else{
            isCache = 0;
        }
    }
    
    [[NSUserDefaults standardUserDefaults] setObject:currentTime forKey:@"getRegionTime"];
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:@{@"method" : @"app.region.all",@"isCache":@(isCache)}];
    NSDictionary *parames = [DataTool addSignToDic:dict];
    [DataTool postWithUrl:HEAD_URL parameters:parames success:^(id data) {
        id backData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments | NSJSONReadingMutableContainers error:nil];
        NSLog(@"地区类型的数据 :%@",backData);
        _allRegionArray = backData[@"data"][@"allRegion"];
        
        for (int i = 0; i<_allRegionArray.count; i++) {
            NSDictionary *provinceDict = _allRegionArray[i];
            if (provinceDict) {
                [_provinceArray addObject:provinceDict];
            }
            
            self.selectedArray = _provinceArray.firstObject[@"city"];
            if (self.selectedArray.count > 0) {
                self.cityArray = _provinceArray.firstObject[@"city"];
            }
            if(self.cityArray.count> 0){
                self.regionArray = self.cityArray.firstObject[@"district"];
            }
        }
    } fail:^(NSError *error) {
        NSLog(@"%@",error.localizedDescription);
    }];
}
- (void)addPickerView
{
    CGFloat pickerH = [UIView getHeight:220.0f];
//    _placeView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, self.height)];
//    _placeView.backgroundColor = [UIColor whiteColor];
//    _placeView.hidden = YES;
//    [self addSubview:_placeView];
    UIView *line1 = [ViewTool getLineViewWith:CGRectMake(0, 0 , kScreenWidth, 1) withBackgroudColor:GRAYLINECOLOR];
    [self addSubview:line1];
    
    UIButton *leftBtn = [[UIButton alloc] initWithFrame:CGRectMake(10, line1.maxY + 5, 50, 20)];
    [leftBtn setTitle:@"取消" forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(hanldePlaceSelected:) forControlEvents:UIControlEventTouchUpInside];
    [leftBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    leftBtn.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    [self addSubview:leftBtn];
    
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(self.width - 10 - 50, line1.maxY + 5, 50, 20)];
    [rightBtn setTitle:@"确定" forState:UIControlStateNormal];
    rightBtn.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    [rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(hanldePlaceSelected:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:rightBtn];
    UIView *line = [ViewTool getLineViewWith:CGRectMake(0, rightBtn.maxY + 5 , kScreenWidth, 1) withBackgroudColor:GRAYLINECOLOR];
    [self addSubview:line];
//    
    _placePickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, line.maxY  , kScreenWidth, _placeView.height - 30)];
    [_placeView addSubview:_placePickerView];
    _placePickerView.delegate = self;
    _placePickerView.dataSource = self;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
 
   return 3;
    
   
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component ==0) {
            return _provinceArray.count;
        }else if(component == 1){
            return _cityArray.count;
        }else{
            return _regionArray.count;
        }
        

    return 0;
    
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
   
        if (component == 0) {
            return _provinceArray[row][@"region"];
        }else if (component == 1) {
            return _cityArray[row][@"region"];
        }else{
            return _regionArray[row][@"region"];
        }
    
    return nil;
    
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0) {
        _selectedArray = self.provinceArray[row][@"city"];
        if (_selectedArray.count > 0) {
            self.cityArray = _selectedArray;
        }else{
            self.cityArray = nil;
        }
        if (self.cityArray.count > 0) {
            self.regionArray = _cityArray.firstObject[@"district"];
        } else {
            self.regionArray = nil;
        }
    }
    [pickerView selectedRowInComponent:1];
    [pickerView reloadComponent:1];
    [pickerView selectedRowInComponent:2];
    if (component == 1) {
        if (self.selectedArray.count > 0 && self.cityArray.count > 0) {
            self.regionArray = [self.selectedArray objectAtIndex:row][@"district"];
            //   self.cityArray = _provinceArray.firstObject[@"city"];
        } else {
            self.regionArray = nil;
        }
        [pickerView selectRow:1 inComponent:2 animated:YES];
    }
    [pickerView reloadComponent:2];
    
    //.........
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
